<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <!--<el-form-item label="任务信息：">-->
        <!--<el-button v-for="(item,index) in menuTypeList" v-if="index!=0" @click="menuTypeChange(item.value)" :key="index"-->
                   <!--:class="{'selectedConfType':menuType==item.value}">-->
          <!--{{item.name}}-->
        <!--</el-button>-->
      <!--</el-form-item>-->
      <br/>
      <el-form-item label="筛选条件：">
        <el-input v-model="dataForm.keyword.name" placeholder="输入任务名称查找" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('picbook:pbtaskgroup:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        align="center"
        label="编号"
        prop="id"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="任务名称">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center" width="200"
        label="创建时间">
        <template slot-scope="scope">
          创建时间<br/>{{scope.row.createTime}}<br/>
          <div v-if="scope.row.updateTime"><br/>修改时间<br/>{{scope.row.updateTime}}</div>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="任务生效时间">
        <template slot-scope="scope">
          {{handleStrategy(scope.row.strategy)}}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="left"
        label="任务内容">
        <template slot-scope="scope" v-if="scope.row.itemList">
          <div v-for="(item,index) in scope.row.itemList" :key="index">
            <div>
              任务{{index}}：{{item.itemName}}
            </div>
            <template v-if="item.rewards">
              奖励：
              <span v-for="(subItem,subIndex) in item.rewards" :key="subIndex">
                {{subItem.rewardName}}x{{subItem.count}}{{subIndex==item.rewards.length-1?'':' + '}}
              </span>
            </template>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center" width="50"
        label="状态">
        <template slot-scope="scope">
          {{scope.row.status==1?'上架':'下架'}}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="modifyStatus(scope.row.id,scope.row.status==0?'1':'0')">
            {{scope.row.status==0?'上架':'下架'}}
          </el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './pbtaskgroup-add-or-update'

  export default {
    data () {
      return {
        menuType: '1',
        weekArr: ['日', '一', '二', '三', '四', '五', '六'],
        dataForm: {
          keyword: {
            name: ''
          }
        },
        menuTypeList: [
          {
            index: 0,
            value: '1',
            name: '每日任务'
          }],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      menuTypeChange (value) {
        this.menuType = value
        this.getDataList(1)
      },
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/pbtaskgroup/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        this.$confirm(`确定[${id ? '删除' : '批量删除'}]?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/picbook/pbtaskgroup/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      handleStrategy (strategy) {
        var ret = ''
        if (strategy) {
          var arr = strategy.split(',')
          for (let i = 0; i < arr.length; i++) {
            if (i == 0) {
              ret = '每周'
            }
            ret += (this.weekArr[arr[i] - 1] + (i == arr.length - 1 ? '' : '、'))
          }
          return ret
        }
      },
      //上下架
      modifyStatus (id, status) {
        this.$http({
          url: this.$http.adornUrl('/picbook/pbtaskgroup/modifyStatus'),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
            'status': status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
    }
  }
</script>
